package com.yjhb.android.sbz.activity;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.EverydayWelfareAdapter;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.EverydayWelfare;
import com.yjhb.android.sbz.mvp.everydaywelfare.EverydayWelfarePresenter;
import com.yjhb.android.sbz.mvp.everydaywelfare.EverydayWelfareView;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.jojo.android.zxlib.tool.ZxSpanTool;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.iwgang.countdownview.CountdownView;

/**
 * 创建者 zx
 * 创建时间 2021/1/5 9:46
 */
public class EverydayWelfareActivity extends BaseActivity<EverydayWelfarePresenter> implements EverydayWelfareView, BaseRecyclerAdapter.ItemClickListener<EverydayWelfare> {
    private final String TAG = "EverydayWelfareActivity";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    EverydayWelfareAdapter adapter;
    @BindView(R.id.countdownView)
    CountdownView countdownView;
    @BindView(R.id.title_tv)
    TextView title_tv;

    List<EverydayWelfare> everydayWelfares = new ArrayList<>();

    @Override
    protected EverydayWelfarePresenter initPresenter() {
        return new EverydayWelfarePresenter(this);
    }

    @Override
    protected boolean isCheckLogin() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_everyday_welfare;
    }

    @Override
    protected void initUI() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        recyclerView.setAdapter(adapter = new EverydayWelfareAdapter(this));
        adapter.setData(everydayWelfares);
        String tip = "最高188元红包!!";
        String highLight = "188元";
        int index= tip.indexOf(highLight);
        title_tv.setText(ZxSpanTool.ColorSpan(tip,index,index+highLight.length(), Color.YELLOW));
    }

    private void downTime() {
        if (countdownView != null) {
            countdownView.stop();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        long currTime = System.currentTimeMillis();
        long time = calendar.getTimeInMillis() - currTime;
        countdownView.setOnCountdownEndListener(new CountdownView.OnCountdownEndListener() {
            @Override
            public void onEnd(CountdownView cv) {
                downTime();
            }
        });
        countdownView.start(time);
    }


    @Override
    protected void initData() {
        presenter.load();
    }

    @Override
    protected void regEvent() {
        adapter.setOnItemClick(this);
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
    }

    @Override
    public void onWait(String tips) {
        loadingDialog.setTipsText(tips);
        loadingDialog.show();
    }

    @Override
    public void onLoad(List<EverydayWelfare> welfares) {
        loadingDialog.cancel();
        everydayWelfares.clear();
        everydayWelfares.addAll(welfares);
        adapter.notifyDataSetChanged();
        countdownView.setVisibility(View.VISIBLE);
        downTime();
    }

    @Override
    public void onSubmit(Object o) {
        loadingDialog.cancel();
        ZxToast.showINFOBottomToast("领取成功！");
    }

    @Override
    public void onItemClick(int index, EverydayWelfare everydayWelfare) {

    }

    @Override
    public void onViewClick(View view, String msg, int index, EverydayWelfare everydayWelfare) {
        if (msg.equals("领取")) {
            presenter.getDailyTaskReward(everydayWelfare.getId());
        } else if (msg.equals("去完成")) {
            ZxActivityTool.skipActivity(this, MainActivity.class);
            if (everydayWelfare.getTaskType() == 1) {
                EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_MAIN_SWITCH_FRAGMENT, 1));
            } else if (everydayWelfare.getTaskType() == 2) {
                EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_MAIN_SWITCH_FRAGMENT, 3));
            }
        }
    }
}
